package com.aoip.telemetry.telemetry.core.call;

import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.aoip.telemetry.telemetry.entity.equipment.QuerySetOrderEntity;
import com.aoip.telemetry.telemetry.entity.order.OrderBaseEntity;
import com.aoip.telemetry.telemetry.entity.order.RelOrderElementEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2023/5/2 16:45
 */
@Service
public class GenerateOrder {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Resource
    private RedisCache redisCache;

    public QuerySetOrderEntity generate(List<String> msgList, String stCode,  OrderBaseEntity order){
        QuerySetOrderEntity entity = new QuerySetOrderEntity();
        if (order == null) {
            return null;
        }
        List<RelOrderElementEntity> elementEntityList = order.getElementEntityList();
        if (elementEntityList == null) {
            return null;
        }
        String msg = createMsg(msgList, stCode, elementEntityList);
        entity.setOrderBaseId(order.getId());
        entity.setStCode(stCode);
        entity.setAllMsg(msg);
        entity.setMsgType(order.getMsgFormatType());
        return entity;
    }

    private String createMsg(List<String> msgList, String stCode, List<RelOrderElementEntity> elementEntityList){
        StringBuilder sb = new StringBuilder();
        elementEntityList.forEach(item -> {
            String elementType = item.getElementType();
            int elementOrder = item.getElementOrder();
            String elementValue = item.getElementValue();
            switch (elementType) {
                case "fixed_char":
                case "crc":
                    sb.append(elementValue);
                    break;
                case "time_char":
                    LocalDateTime localDateTime = LocalDateTime.parse(msgList.get(elementOrder), DATE_TIME_FORMATTER);
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(elementValue);
                    sb.append(localDateTime.format(formatter));
                    break;
                case "auto_fill":
                    if ("stCode".equals(elementValue)) {
                        sb.append(stCode);
                    } else if ("cn".equals(elementValue)) {
                        String key = CacheKeyEnum.ORDER_SEND_CN + "";
                        int cn = 0;
                        if (redisCache.hasKey(key)) {
                            cn = redisCache.getCacheObject(key);
                        }
                        redisCache.setCacheObject(key, cn + 1);
                        sb.append(cn);
                    }
                    break;
                case "user_input":
                    sb.append(msgList.get(elementOrder));
                    break;
                default:
                    break;
            }
        });
        return sb.toString();
    }
}
